{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):  # Exclusive of 10\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(-10, 10):  # Exclusive of upper bound, inclusive of lower bound\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(\n",
    "    -10, 10, 20\n",
    "):  # Exclusive of upper bound, inclusive of lower bound, step of 2\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_range = range(10)\n",
    "\n",
    "print(type(my_range))  # range(0, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections.abc import Sequence\n",
    "\n",
    "print(issubclass(range, Sequence))  # Pylance even complains about this, but it's true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_range = range(10)\n",
    "\n",
    "\n",
    "print(f\"Fetching the first element: {my_range[0]}\")\n",
    "print(f\"Fetching the last element: {my_range[-1]}\")\n",
    "print(f\"Fetching the second element: {my_range[1]}\")\n",
    "print(f\"Length of the range: {len(my_range)}\")\n",
    "print(f\"Index of 5: {my_range.index(5)}\")\n",
    "print(f\"Count of 5: {my_range.count(5)}\")\n",
    "print(f\"Slice of the range: {my_range[2:5]}\")  # This returns a range object\n",
    "print(f\"Slice of the range: {my_range[2:5:2]}\")  # This returns a range object\n",
    "print(f\"Reversed range: {list(reversed(my_range))}\")\n",
    "print(f\"min of the range: {min(my_range)}\")\n",
    "print(f\"max of the range: {max(my_range)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sys import getsizeof\n",
    "\n",
    "\n",
    "my_range = range(0, 20, 2)\n",
    "my_list = list(my_range)\n",
    "\n",
    "print(getsizeof(my_range))\n",
    "print(getsizeof(my_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = range(10, 0, -2)\n",
    "print(list(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = range(10, 20, -2)\n",
    "print(list(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python int too large to convert to C ssize_t\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "\n",
    "# sys.maxsize on most platforms is 2**63 - 1 (for 64-bit systems)\n",
    "large_range = range(-sys.maxsize, sys.maxsize)\n",
    "\n",
    "try:\n",
    "    print(len(large_range))\n",
    "except OverflowError as e:\n",
    "    print(e)  # Output: cannot fit 'int' into an index-sized integer"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
